package service

import (
	"net/http"
	"html/template"
	"strconv"
	
	"github.com/unrolled/render"
)

func formHandler(formatter *render.Render) http.HandlerFunc {

	return func(w http.ResponseWriter, req *http.Request) {
		req.ParseForm()
		fnum, err1 := strconv.Atoi(template.HTMLEscapeString(req.Form.Get("fnum")))
		lnum, err2 := strconv.Atoi(template.HTMLEscapeString(req.Form.Get("lnum")))
		var sum int
		if err1 != nil || err2 != nil{
			sum = 0
		}else{
			sum = fnum + lnum
		}
		formatter.HTML(w, http.StatusOK, "output", struct {
			Fnum      int `json:"fnum"`
			Lnum 	  int `json:"lnum"`
			Sum		  int `json:"sum"`
		}{Fnum: fnum, Lnum: lnum, Sum: sum})
	}
}